package com.maduar.demo.algo.dp;

/**
 * Description:
 *
 * @author maduar maduar@163.com
 * @date 2021-03-27 00:44
 */
public class DpMore {

    public static int fibonacci(int n) {
        if (n == 1) {
            return 1;
        } else if (n == 2) {
            return 1;
        }

        int a = 1;
        int b = 2;
        int result = a + b;
        for (int i = 3; i <= n; i++) {
            result = a + b;
            a = b;
            b = result;
        }

        return result;
    }

    public static int errorLetter(int n) {
        if (n == 1) {
            return 0;
        }

        if (n == 2) {
            return 1;
        }

        // dp[i] = (i - 1)*(dp[i - 1] + dp[i - 2]
        int[] arr = new int[n + 1];
        arr[1] = 0;
        arr[2] = 1;

        for (int i = 3; i <= n; i++) {
            arr[i] = (i - 1) * (arr[i - 1] + arr[i - 2]);
        }

        return arr[n];
    }
}
